/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.personal.service.impl;

import com.koron.common.bean.StaffBean;
import com.koron.common.emailandsms.SendEmail;
import com.koron.common.web.Util;
import com.koron.personal.domain.MyCertificate;
import com.koron.personal.mapper.MyCertificateMapper;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyCertificateJob implements Job {
    private final static  Logger logger= LoggerFactory.getLogger(MyCertificateJob.class);
    @Autowired
    private Util util;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail detail = context.getJobDetail();
        String id = detail.getJobDataMap().getString("id");
        try(SessionFactory factory=new SessionFactory()){
            MyCertificate myCertificate=factory.getMapper(MyCertificateMapper.class).queryOne(id);
            if (null!=myCertificate){
                String staffCode=myCertificate.getStaffCode();
                List<StaffBean> staffBeans= (List<StaffBean>) util.staff(staffCode,null).getData();
                if (null!=staffBeans&&staffBeans.size()>0){
                    if (StringUtils.isNotEmpty(staffBeans.get(0).getEmail())){
                        String [] to=new String[]{staffBeans.get(0).getEmail()};
                        String text="证件名称为："+myCertificate.getCertificateName()+"将在7天后过期，请及时上传新的证件";
                        String subject="证件过期提醒";
                        SendEmail.simpleMailSend(to,text,subject);
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }
}
